﻿'<Importaciones de Bibliotecas Generales>
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections
Imports System.Diagnostics
Imports System.Data.Common
Imports System.Object

'<Importaciones de Bibliotecas Generales Prácticas Recomendadas>
Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data

Public Class Seguridad
    Public Shared Function VerificarRolPermisoUsuario(ByVal Usuario As String, ByVal modulo As String) As String()
        Try
            Dim RolDS, PermisoDS As New Data.DataSet
            Dim Rol As String = ("")
            Dim Permiso As String = ("")
            Dim cedula As String = ("")
            Dim mail As String = ("")

            'Llenado del DataSet para Obtener el Rol del Usuario()

            Dim db As Database = DatabaseFactory.CreateDatabase("Litigioso.My.MySettings.CnxnSeguridad")
            Dim cmdRol As DbCommand = db.GetStoredProcCommand("SP_CONSULTA_ROL")
            db.AddInParameter(cmdRol, "@USUARIO", DbType.String, Usuario)
            db.AddInParameter(cmdRol, "@COD_MODULO", DbType.String, modulo)
            db.LoadDataSet(cmdRol, RolDS, "ROL_USUARIO")

            'Obtengo el Rol del Usuario 
            If RolDS.Tables(0).Rows.Count > 0 Then
                Rol = RolDS.Tables(0).Rows(0).Item(2).ToString
                cedula = RolDS.Tables(0).Rows(0).Item(3).ToString
                mail = RolDS.Tables(0).Rows(0).Item(4).ToString
            Else
                Rol = 0
            End If

            Dim Credenciales() As String = New String(4) {}
            Credenciales(0) = Rol
            Credenciales(2) = cedula
            Credenciales(3) = mail

            'Si existe un rol asociado a ese usuario entra al IF
            If Rol.Length > 0 Then
                'Llenado del DataSet para Obtener el permiso del Usuario segun el Rol obtenido
                Dim db1 As Database = DatabaseFactory.CreateDatabase("Litigioso.My.MySettings.CnxnSeguridad")
                Dim cmdPermiso As DbCommand = db1.GetStoredProcCommand("SP_CONSULTA_PANTALLAS_POR_ROL")
                db1.AddInParameter(cmdPermiso, "@COD_MODULO", DbType.String, modulo)
                db1.AddInParameter(cmdPermiso, "@COD_ROL", DbType.String, Rol)
                db1.LoadDataSet(cmdPermiso, PermisoDS, "PERMISO_USUARIO")

                For i As Integer = 0 To PermisoDS.Tables(0).Rows.Count - 1
                    If UCase(PermisoDS.Tables(0).Rows(i).Item(2).ToString) = UCase("PAGBienvenida") Then
                        Permiso = IIf(PermisoDS.Tables(0).Rows(i).Item(3).ToString = Nothing, 0, PermisoDS.Tables(0).Rows(i).Item(3).ToString)
                        Credenciales(1) = Permiso
                    End If
                Next
            Else
                Credenciales(1) = 0
            End If
            Return Credenciales
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Shared Function ObtenerRol(ByVal usuario As String, ByVal modulo As String) As DataSet
        Dim Ds As New DataSet
        Try
            Dim db As Database = DatabaseFactory.CreateDatabase("Litigioso.My.MySettings.CnxnSeguridad")
            'DsPermisos_Usuario
            Dim comando As DbCommand = db.GetStoredProcCommand("SP_CONSULTA_ROL")
            db.AddInParameter(comando, "@USUARIO", DbType.String, usuario)
            db.AddInParameter(comando, "@COD_MODULO", DbType.String, modulo)
            db.LoadDataSet(comando, Ds, "PERMISOS_USUARIO")

        Catch ex As Exception
            Throw ex
        End Try
        Return Ds
    End Function
  
    Public Shared Function ObtenerObjetosPorRol(ByVal modulo As String, ByVal rol As String, ByVal pantalla As String) As DataSet
        Dim DS As New DataSet
        Try
            Dim db As Database = DatabaseFactory.CreateDatabase("Litigioso.My.MySettings.CnxnSeguridad")
            Dim comando As DbCommand = db.GetStoredProcCommand("SP_CONSULTA_OBJETOS_POR_ROL_POR_PANTALLA")
            db.AddInParameter(comando, "@COD_MODULO", DbType.String, modulo)
            db.AddInParameter(comando, "@COD_ROL", DbType.String, rol)
            db.AddInParameter(comando, "@COD_PANTALLA", DbType.String, pantalla)
            db.LoadDataSet(comando, DS, "OBJETOS_POR_ROL")

        Catch ex As Exception
            Throw ex
        End Try

        Return DS
    End Function

    Public Shared Function ObtenerPantallasXmoduloXrol(ByVal modulo As String, ByVal rol As String) As DataSet
        Dim Ds As New DataSet
        Try
            Dim db As Database = DatabaseFactory.CreateDatabase("Litigioso.My.MySettings.CnxnSeguridad")
            Dim comando As DbCommand = db.GetStoredProcCommand("SP_CONSULTA_PANTALLAS_POR_ROL")
            db.AddInParameter(comando, "@COD_MODULO", DbType.String, modulo)
            db.AddInParameter(comando, "@COD_ROL", DbType.String, rol)
            db.LoadDataSet(comando, Ds, "PANTALLAS_POR_ROL")

        Catch ex As Exception
            Throw ex
        End Try

        Return Ds
    End Function


End Class
